#!/bin/sh

# AM INSTALL SCRIPT VERSION 3. 
set -u
APP=libreoffice
SITE="https://www.libreoffice.org"

# CREATE DIRECTORIES AND ADD REMOVER
[ -n "$APP" ] && mkdir -p "/opt/$APP/icons" "/opt/$APP/tmp" && cd "/opt/$APP/tmp" || exit 1
printf "#!/bin/sh\nset -e\nrm -f /usr/local/bin/$APP\nrm -R -f /opt/$APP" > "/opt/$APP/remove"
printf '\n%s' "rm -f /usr/local/share/applications/$APP*-AM.desktop" >> "/opt/$APP/remove"
chmod a+x "/opt/$APP/remove"

_release_still() {
	lovariant="still"
	version=$(curl -Ls https://api.github.com/repos/ivan-hc/LibreOffice-appimage/releases | sed 's/[()",{} ]/\n/g' | grep -oi "https.*continuous-still.*$edition.*mage$" | head -1)
}

_release_fresh() {
	lovariant="fresh"
	version=$(curl -Ls https://api.github.com/repos/ivan-hc/LibreOffice-appimage/releases | sed 's/[()",{} ]/\n/g' | grep -oi "https.*continuous-fresh.*$edition.*mage$" | head -1)
}

_release_junest_still() {
	lovariant="still"
	version=$(curl -Ls https://api.github.com/repos/ivan-hc/LibreOffice-appimage/releases | sed 's/[()",{} ]/\n/g' | grep -oi "https.*continuous-junest-still.*$edition.*mage$" | head -1)
}

_release_junest_fresh() {
	lovariant="fresh"
	version=$(curl -Ls https://api.github.com/repos/ivan-hc/LibreOffice-appimage/releases | sed 's/[()",{} ]/\n/g' | grep -oi "https.*continuous-junest-fresh.*$edition.*mage$" | head -1)
}

# CHOOSE A VERSION

echo " Please choose a LibreOffice edition:

 - STILL is slightly older but tested longer.
 - FRESH includes all the latest features.
 - BASIC only supports English (US and GB).
 - STANDARD includes English (US/GB), Arabic, Chinese (Simplified and
   Traditional), French, German, Italian, Japanese, Korean, Portuguese
   (BR/PT), Russian, and Spanish.
 - FULL includes all supported languages.

 See more at https://github.com/ivan-hc/LibreOffice-appimage

----------------------------------------------------------------------
	APPIMAGE (CLASSIC)	   |	   ARCHIMAGE
-----------------------------------|----------------------------------
				   |
 1) Still-basic			   |	13) Still-basic
 2) Still-basic with manuals	   |	14) Still-italian
 3) Still-standard		   |	15) Still-european
 4) Still-standard with manuals	   |	16) Still-standard
 5) Still-full			   |	17) Still-full
 6) Still-full with manuals	   |
 				   |
 7) Fresh-basic			   |	18) Fresh-basic
 8) Fresh-basic with manuals	   |	19) Fresh-italian
 9) Fresh-standard		   |	20) Fresh-european
 10) Fresh-standard with manuals   |	21) Fresh-standar
 11) Fresh-full			   |	22) Fresh-full
 12) Fresh-full with manuals	   |
 				   |
----------------------------------------------------------------------"
read -r -p " Type a number from the above list and press ENTER: " response
case "$response" in
	1)	edition="basic-x86_64"
		_release_still 2>/dev/null 
		wget "$version"
		;;
	2)	edition="basic.help-x86_64"
		_release_still 2>/dev/null 
		wget "$version"
		;;
	3)	edition="standard-x86_64"
		_release_still 2>/dev/null 
		wget "$version"
		;;
	4)	edition="standard.help-x86_64"
		_release_still 2>/dev/null 
		wget "$version"
		;;
	5)	edition="full-x86_64"
		_release_still 2>/dev/null 
		wget "$version"
		;;
	6)	edition="full.help-x86_64"
		_release_still 2>/dev/null 
		wget "$version"
		;;
	7)	edition="basic-x86_64"
		_release_fresh 2>/dev/null 
		wget "$version"
		;;
	8)	edition="basic.help-x86_64"
		_release_fresh 2>/dev/null 
		wget "$version"
		;;
	9)	edition="standard-x86_64"
		_release_fresh 2>/dev/null 
		wget "$version"
		;;
	10)	edition="standard.help-x86_64"
		_release_fresh 2>/dev/null 
		wget "$version"
		;;
	11)	edition="full-x86_64"
		_release_fresh 2>/dev/null 
		wget "$version"
		;;
	12)	edition="full.help-x86_64"
		_release_fresh 2>/dev/null 
		wget "$version"
		;;
	13)	edition="basic"
		_release_junest_still 2>/dev/null 
		wget "$version"
		;;
	14)	edition="italian"
		_release_junest_still 2>/dev/null 
		wget "$version"
		;;
	15)	edition="european"
		_release_junest_still 2>/dev/null 
		wget "$version"
		;;
	16)	edition="standard"
		_release_junest_still 2>/dev/null 
		wget "$version"
		;;
	17)	edition="full"
		_release_junest_still 2>/dev/null 
		wget "$version"
		;;
	18)	edition="basic"
		_release_junest_fresh 2>/dev/null 
		wget "$version"
		;;
	19)	edition="italian"
		_release_junest_fresh 2>/dev/null 
		wget "$version"
		;;
	20)	edition="european"
		_release_junest_fresh 2>/dev/null 
		wget "$version"
		;;
	21)	edition="standard"
		_release_junest_fresh 2>/dev/null 
		wget "$version"
		;;
	22)	edition="full"
		_release_junest_fresh 2>/dev/null 
		wget "$version"
		;;
	''|*)	exit 1
		;;
esac
cd ..
mv ./tmp/*mage ./"$APP"

rm -R -f ./tmp || exit 1
echo "$version" > ./version
chmod a+x ./"$APP" || exit 1

# LINK
ln -s "/opt/$APP/$APP" "/usr/local/bin/$APP"

# SCRIPT TO UPDATE THE PROGRAM
cat >> "/opt/$APP/AM-updater" << 'EOF'
#!/bin/sh
set -u
APP=libreoffice
SITE="https://www.libreoffice.org"
version0=$(cat "/opt/$APP/version")
version=$(curl -Ls https://api.github.com/repos/ivan-hc/LibreOffice-appimage/releases | sed 's/[()",{} ]/\n/g' | grep -oi "https.*mage$" | grep -i "LOVARIANT" | grep -i "EDITION" | grep -vi archimage | head -1)
if command -v appimageupdatetool >/dev/null 2>&1; then
	cd "/opt/$APP" || exit 1
	appimageupdatetool -Or ./"$APP" && chmod a+x ./"$APP" && echo "$version" > ./version && exit 0
fi
if [ "$version" != "$version0" ]; then
	mkdir "/opt/$APP/tmp" && cd "/opt/$APP/tmp" || exit 1
	notify-send "A new version of $APP is available, please wait"
	wget "$version" || exit 1
	# Use tar fx ./*tar* here for example in this line in case a compressed file is downloaded.
	cd ..
	mv --backup=t ./tmp/*mage ./"$APP"
	chmod a+x ./"$APP" || exit 1
	echo "$version" > ./version
	rm -R -f ./*zs-old ./*.part ./tmp ./*~
	notify-send "$APP is updated!"
else
	echo "Update not needed!"
fi
EOF
sed -i "s#EDITION#$edition#g; s#LOVARIANT#$lovariant#g" "/opt/$APP/AM-updater"
if echo "$version" | grep -qi "archimage"; then	sed -i "s/vi archimage/i archimage/g" "/opt/$APP/AM-updater"; fi
chmod a+x "/opt/$APP/AM-updater"

# ICONS
cd "/opt/$APP" || exit 1
./libreoffice --appimage-extract usr/share/icons/hicolor/scalable/apps/*.svg && mv squashfs-root/usr/share/icons/hicolor/scalable/apps/*.svg ./icons 2>/dev/null
./libreoffice --appimage-extract .junest/usr/share/icons/hicolor/scalable/apps/*.svg && mv squashfs-root/.junest/usr/share/icons/hicolor/scalable/apps/*.svg ./icons 2>/dev/null
RELEASE=$(ls ./icons | head -1 | rev | cut -c 10- | rev)
mv ./icons/"$RELEASE"-base* ./icons/"$APP"-base 2>/dev/null
mv ./icons/"$RELEASE"-basic* ./icons/"$APP"-basic 2>/dev/null
mv ./icons/"$RELEASE"-calc* ./icons/"$APP"-calc 2>/dev/null
mv ./icons/"$RELEASE"-chart* ./icons/"$APP"-chart 2>/dev/null
mv ./icons/"$RELEASE"-draw* ./icons/"$APP"-draw 2>/dev/null
mv ./icons/"$RELEASE"-extension* ./icons/"$APP"-extension 2>/dev/null
mv ./icons/"$RELEASE"-impress* ./icons/"$APP"-impress 2>/dev/null
mv ./icons/"$RELEASE"-main* ./icons/"$APP"-main 2>/dev/null
mv ./icons/"$RELEASE"-math* ./icons/"$APP"-math 2>/dev/null
mv ./icons/"$RELEASE"-startcenter* ./icons/"$APP"-startcenter 2>/dev/null
mv ./icons/"$RELEASE"-writer* ./icons/"$APP"-writer 2>/dev/null

# LAUNCHERS
./libreoffice --appimage-extract opt/libreoffice*/share/xdg 2>/dev/null
./libreoffice --appimage-extract .junest/usr/lib/libreoffice/share/xdg 2>/dev/null
LAUNCHERS=$(find squashfs-root -name *.desktop -type f | xargs)
for f in $LAUNCHERS ; do
	launcher=$(echo "$f" | sed 's:.*/::')
	mv "$f" ./"$APP-${launcher%.desktop}-AM.desktop"
done
sed -i "s# $(echo $RELEASE | cut -c 12-) # #g; s#Icon=$APP#Icon=/opt/$APP/icons/$APP#g; s#$RELEASE#$APP#g" *.desktop
sed -i '/Name=LibreOffice/c\Name=LibreOffice' *startcenter-AM.desktop
mv *.desktop /usr/local/share/applications/
rm -R -f ./squashfs-root
